<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
if( !defined('SYS_RT_CURRENT_TIME') ) {
	define('SYS_RT_CURRENT_TIME', time());
}

//
function func_stats_get_session_id() {
	global $sessionObj;
	
	if( $sessionObj->session_id ) {
		$sid = $sessionObj->session_id;
	} else {
		$sid = $GLOBALS['PPSID'];
		//$sid = $_COOKIE['PPSID'];
	}
	
	return $sid;
}

//STATS HISTORY
function func_stats_add_stats_history() {
	global $adb, $request_uri;
	
	$users_id = func_db_input($_SESSION['suser']->userid);
	$users_name = func_db_input($_SESSION['suser']->username);
	//
	$session_id = func_stats_get_session_id();
	if( $session_id ) {
		$ip_address = util_get_ip_address();
		$last_page_url = func_db_input($request_uri);
		$xx_mins_ago = (SYS_RT_CURRENT_TIME - 900);
		
		//
		$query = "select count(*) as count from pp_stats_history where session_id = '" . $session_id . "' and ip_address = '" . $ip_address . "' and time_last_click > '" . $xx_mins_ago . "'";
		$result = $adb->query($query);
		$row = $adb->fetch_array($result);
		if( $row['count'] > 0 ) {
			$adb->query("update pp_stats_history set time_last_click = '" . SYS_RT_CURRENT_TIME . "', last_page_url = '" . $last_page_url . "' where session_id = '" . $session_id . "' and ip_address = '" . $ip_address . "'");
		} else {
			$adb->query("insert into pp_stats_history (users_id, users_name, session_id, ip_address, time_entry, time_last_click, last_page_url) values ('" . $users_id . "', '" . $users_name . "', '" . $session_id . "', '" . $ip_address . "', '" . SYS_RT_CURRENT_TIME . "', '" . SYS_RT_CURRENT_TIME . "', '" . $last_page_url . "')");
		}
	}
}

//ONLINELISTS
function func_stats_add_stats_onlinelists() {
	global $adb, $request_uri;
	
	$users_id = func_db_input($_SESSION['suser']->userid);
	$users_name = func_db_input($_SESSION['suser']->username);
	//
	$session_id = func_stats_get_session_id();
	if( $session_id ) {
		$ip_address = util_get_ip_address();
		$last_page_url = func_db_input($request_uri);
		$xx_mins_ago = (SYS_RT_CURRENT_TIME - 900);
		
		//
		$adb->query("delete from pp_stats_onlinelists where time_last_click < '" . $xx_mins_ago . "'");
		
		//
		$query = "select count(*) as count from pp_stats_onlinelists where session_id = '" . $session_id . "' and ip_address = '" . $ip_address . "' and time_last_click > '" . $xx_mins_ago . "'";
		$result = $adb->query($query);
		$row = $adb->fetch_array($result);
		if( $row['count'] > 0 ) {
			$adb->query("update pp_stats_onlinelists set time_last_click = '" . SYS_RT_CURRENT_TIME . "', last_page_url = '" . $last_page_url . "' where session_id = '" . $session_id . "' and ip_address = '" . $ip_address . "'");
		} else {
			$adb->query("insert into pp_stats_onlinelists (session_id, ip_address, users_id, users_name, time_entry, time_last_click, last_page_url) values ('" . $session_id . "', '" . $ip_address . "', '" . $users_id . "', '" . $users_name . "', '" . SYS_RT_CURRENT_TIME . "', '" . SYS_RT_CURRENT_TIME . "', '" . $last_page_url . "')");
		}
	}
}

function func_stats_get_stats_onlinelists() {
	global $adb;
	
	$query = "select * from pp_stats_onlinelists";
	$rows = $adb->getRows($query);
	
	return $rows;
}

function func_stats_get_stats_onlinecount() {
	global $adb, $SETTINGS;
	
	$query = "select count(*) as count from pp_stats_onlinelists";
	$result = $adb->query($query);
	$row = $adb->fetch_array($result);
	$counter = $row['count'];
	
	if( $SETTINGS['counter_stats_base'] > 0 ) {
		$counter += $SETTINGS['counter_stats_base'];
	}
	
	return $counter;
}

//ONLINELISTS ALIAS
function func_stats_add_onlinelists() {
	global $adb, $request_uri;
	
	$users_id = func_db_input($_SESSION['suser']->userid);
	$users_name = func_db_input($_SESSION['suser']->username);
	//
	$session_id = func_stats_get_session_id();
	if( $session_id ) {
		$ip_address = util_get_ip_address();
		$last_page_url = func_db_input($request_uri);
		$xx_mins_ago = (SYS_RT_CURRENT_TIME - 900);
		
		//
		$adb->query("delete from pp_stats_onlinelists where time_last_click < '" . $xx_mins_ago . "'");
		
		//
		$query = "select count(*) as count from pp_stats_onlinelists where session_id = '" . $session_id . "' and ip_address = '" . $ip_address . "' and time_last_click > '" . $xx_mins_ago . "'";
		$result = $adb->query($query);
		$row = $adb->fetch_array($result);
		if( $row['count'] > 0 ) {
			$adb->query("update pp_stats_onlinelists set time_last_click = '" . SYS_RT_CURRENT_TIME . "', last_page_url = '" . $last_page_url . "' where session_id = '" . $session_id . "' and ip_address = '" . $ip_address . "'");
		} else {
			$adb->query("insert into pp_stats_onlinelists (session_id, ip_address, users_id, users_name, time_entry, time_last_click, last_page_url) values ('" . $session_id . "', '" . $ip_address . "', '" . $users_id . "', '" . $users_name . "', '" . SYS_RT_CURRENT_TIME . "', '" . SYS_RT_CURRENT_TIME . "', '" . $last_page_url . "')");
		}
	}
}

function func_stats_get_onlinelists() {
	global $adb;
	
	$query = "select * from pp_stats_onlinelists";
	$rows = $adb->getRows($query);
	
	return $rows;
}

function func_stats_get_onlinecount() {
	global $adb, $SETTINGS;
	
	$query = "select count(*) as count from pp_stats_onlinelists";
	$result = $adb->query($query);
	$row = $adb->fetch_array($result);
	$counter = $row['count'];
	
	if( $SETTINGS['counter_stats_base'] > 0 ) {
		$counter += $SETTINGS['counter_stats_base'];
	}
	
	return $counter;
}

//STATS
function func_stats_init() {
	func_stats_add('hits');
	func_stats_add('os');
	func_stats_add('browser');
	func_stats_add('week');
	func_stats_add('hour');
}

function func_stats_add($type) {
	if( $type == 'total' ) {
		func_stats_add_total();
	} elseif( $type == 'os' ) {
		func_stats_add_os();
	} elseif( $type == 'browser' ) {
		func_stats_add_browser();
	} elseif( $type == 'week' ) {
		func_stats_add_week();
	} elseif( $type == 'hour' ) {
		func_stats_add_hour();
	}
}

function func_stats_add_total() {
	global $adb;
	
	$adb->query("update pp_stats set stats_count = stats_count + 1 where stats_type = 'total' and stats_name = 'hits'");
	if( $_SESSION['suser']->userid > 0 ) {
		$adb->query("update pp_stats set stats_count = stats_count + 1 where stats_type = 'total' and stats_name = 'members'");
	} else {
		$adb->query("update pp_stats set stats_count = stats_count + 1 where stats_type = 'total' and stats_name = 'guests'");
	}
}

function func_stats_add_os() {
	
}

function func_stats_add_browser() {
	$browser = '';
	$browsers = array('MSIE', 'Netscape', 'Opera', 'Konqueror', 'Mozilla', 'Lynx');
	for($i=0, $n=sizeof($browsers); $i<$n; $i++) {
		if( util_browser_detect($browsers[$i]) ) {
			$browser = $browsers[$i];
			break;
		}
	}
	if( $browser == '' ) {
		$browser = 'Other';
	}
	
	global $adb;
	$adb->query("update pp_stats set stats_count = stats_count + 1 where stats_type = 'week' and stats_name = '" . $browser . "'");
}

function func_stats_add_week() {
	global $adb;
	$day_week = date("w", SYS_RT_CURRENT_TIME);
	$adb->query("update pp_stats set stats_count = stats_count + 1 where stats_type = 'week' and stats_name = '" . $day_week . "'");
}

function func_stats_add_hour() {
	global $adb;
	$hour_day = date("h", SYS_RT_CURRENT_TIME);
	$adb->query("update pp_stats set stats_count = stats_count + 1 where stats_type = 'hour' and stats_name = '" . $hour_day . "'");
}
//
?>